/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.medbeans.modules.search.types;


import java.awt.Color;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.util.Date;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;

import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;


/**
 * Customizer of criterion for testing date of primary file modification.
 *
 * @author  Petr Kuzel
 */
public class ModificationDateCustomizer extends JPanel implements Customizer {

    private ModificationDateType peer;
    private String lastDays = "";
    private String lastBefore = "";
    private String lastAfter = "";

    /** Creates new form ModificationDateCustomizer */
    public ModificationDateCustomizer() {

        initComponents ();   
        initAccessibility();
        TitledBorder tb = new TitledBorder(
            NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_MODIFIED")); // NOI18N
        
        tb.setBorder(new CompoundBorder());
        setBorder (tb);
        
        ButtonGroup group = new ButtonGroup();
        group.add(daysRadioButton);
        group.add(betweenRadioButton);

        HelpCtx.setHelpIDString(this, ModificationDateType.class.toString());
    }

    /** Initialize customizer state.
    */
    public void setObject(final Object obj) {

        peer = (ModificationDateType) obj;

        // set default coloring
        daysTextField.setForeground(Color.black);
        afterTextField.setForeground(Color.black);
        beforeTextField.setForeground(Color.black);

        daysTextField.setText(""); // NOI18N
        afterTextField.setText(""); // NOI18N
        beforeTextField.setText(""); // NOI18N


        // display object value in customizer
        if(!"".equals(peer.getMatchAfter())) { // NOI18N
            afterTextField.setText(peer.getMatchAfter());
            if(!peer.getMatchAfter().equals(null))
                betweenRadioButton.setSelected(true);
        }

        if(!"".equals(peer.getMatchBefore())) { // NOI18N
            beforeTextField.setText(peer.getMatchBefore());
            if(!peer.getMatchAfter().equals(null))
                betweenRadioButton.setSelected(true);
        }

        if(!"".equals(peer.getDays())) { // NOI18N
            daysTextField.setText(peer.getDays());
            daysRadioButton.setSelected(true);
        }

    }

    public void addPropertyChangeListener(final PropertyChangeListener p1) {
    }

    public void removePropertyChangeListener(final PropertyChangeListener p1) {
    }


     private void initAccessibility(){      
        this.getAccessibleContext().setAccessibleName(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACS_Dates"));
        daysTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACSN_DURING_DAYS"));
        daysTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACSD_DURING_DAYS"));
        afterTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACSN_AFTER")); 
        afterTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACSD_AFTER")); 
        beforeTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACSN_BEFORE")); 
        beforeTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACSD_BEFORE")); 
        daysRadioButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACS_TEXT_BUTTON_PRECEDING")); 
        betweenRadioButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(ModificationDateCustomizer.class).getString("ACS_TEXT_BUTTON_RANGE")); 
    }
     
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        daysRadioButton = new javax.swing.JRadioButton();
        daysLabel = new javax.swing.JLabel();
        daysTextField = new javax.swing.JTextField();
        betweenRadioButton = new javax.swing.JRadioButton();
        beginLabel = new javax.swing.JLabel();
        beginLabel.setDisplayedMnemonic(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_BEGIN_DATE_MNEM").charAt(0));
        afterTextField = new javax.swing.JTextField();
        beforeTextField = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        egLabel = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        daysRadioButton.setMnemonic(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_BUTTON_PREC_MNEM").charAt(0));
        daysRadioButton.setSelected(true);
        daysRadioButton.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_BUTTON_PRECEDING"));
        daysRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                daysRadioButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 4);
        add(daysRadioButton, gridBagConstraints);

        daysLabel.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_DURING_LAST"));
        daysLabel.setLabelFor(daysTextField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
        add(daysLabel, gridBagConstraints);

        daysTextField.setPreferredSize(new java.awt.Dimension (50, daysTextField.getPreferredSize ().height));
        daysTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                daysTextFieldKeyReleased(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        add(daysTextField, gridBagConstraints);

        betweenRadioButton.setMnemonic(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_BUTTON_RANGE_MNEM").charAt(0));
        betweenRadioButton.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_BUTTON_RANGE"));
        betweenRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                betweenRadioButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 8, 0, 4);
        add(betweenRadioButton, gridBagConstraints);

        beginLabel.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_BEGIN_DATE"));
        beginLabel.setLabelFor(beforeTextField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        add(beginLabel, gridBagConstraints);

        afterTextField.setPreferredSize(new java.awt.Dimension (100, afterTextField.getPreferredSize ().height));
        afterTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                afterTextFieldKeyReleased(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
        add(afterTextField, gridBagConstraints);

        beforeTextField.setPreferredSize(new java.awt.Dimension (100, beforeTextField.getPreferredSize ().height));
        beforeTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                beforeTextFieldKeyReleased(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 12);
        add(beforeTextField, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanel1, gridBagConstraints);

        egLabel.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_DATE_EXAMPLE") + new ModificationDateType.FormattedDate(new Date()).toString());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 8, 0, 0);
        add(egLabel, gridBagConstraints);

    }//GEN-END:initComponents

    private void beforeTextFieldKeyReleased (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_beforeTextFieldKeyReleased
        String text = beforeTextField.getText();
        if (!text.equals(lastBefore)) {
            setBetween();
            betweenRadioButton.setSelected(true);
            lastBefore = text;
        }
    }//GEN-LAST:event_beforeTextFieldKeyReleased

    private void afterTextFieldKeyReleased (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_afterTextFieldKeyReleased
        String text = afterTextField.getText();
        if (!text.equals(lastAfter)) {
            setBetween();
            betweenRadioButton.setSelected(true);
            lastAfter = text;
        }
    }//GEN-LAST:event_afterTextFieldKeyReleased

    private void betweenRadioButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_betweenRadioButtonActionPerformed
        setBetween();
    }//GEN-LAST:event_betweenRadioButtonActionPerformed

    private void daysRadioButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_daysRadioButtonActionPerformed
        setDays();
    }//GEN-LAST:event_daysRadioButtonActionPerformed

    private void daysTextFieldKeyReleased (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_daysTextFieldKeyReleased
        String text = daysTextField.getText();
        if (!text.equals(lastDays)) {
            setDays();
            daysRadioButton.setSelected(true);
            lastDays = text;
        }
    }//GEN-LAST:event_daysTextFieldKeyReleased

    private void setDays() {

        String text = daysTextField.getText();
        try {
            peer.setDays(text);
            daysTextField.setForeground(Color.black);
        } catch (IllegalArgumentException ex) {
            daysTextField.setForeground(Color.red);
        }
    }

    private void setBetween() {

        String after = afterTextField.getText();
        String before = beforeTextField.getText();

        try {
            peer.setMatchAfter(after);
            afterTextField.setForeground(Color.black);
        } catch (IllegalArgumentException ex) {
            afterTextField.setForeground(Color.red);
        }

        try {
            peer.setMatchBefore(before);
            beforeTextField.setForeground(Color.black);
        } catch (IllegalArgumentException ex) {
            beforeTextField.setForeground(Color.red);
        }

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel daysLabel;
    private javax.swing.JRadioButton daysRadioButton;
    private javax.swing.JTextField daysTextField;
    private javax.swing.JLabel beginLabel;
    private javax.swing.JRadioButton betweenRadioButton;
    private javax.swing.JTextField beforeTextField;
    private javax.swing.JTextField afterTextField;
    private javax.swing.JLabel egLabel;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    public void requestFocus() {
        JTextField tf = betweenRadioButton.isSelected() ?
                          afterTextField : daysTextField;
        int n = tf.getText().length();
        if (n > 0) {
            tf.setCaretPosition(0);
            tf.moveCaretPosition(n);
        }
        tf.requestFocus();
    }
}
